package com.fairytask.logic
{
	import com.fairytask.data.Modulator;
	import com.fairytask.event.LogicEvent;
	import com.fairytask.net.CustomToClientEvent;
	import com.fairytask.net.CustomToServerRequest;
	import com.fairytask.net.NetSocket;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.MainUI;
	import com.fairytask.ui.login.CreateRoleUI;
	import com.smartfoxserver.v2.core.SFSEvent;
	import com.smartfoxserver.v2.entities.SFSUser;
	import com.smartfoxserver.v2.entities.data.ISFSObject;
	import com.smartfoxserver.v2.entities.data.SFSObject;
	import com.smartfoxserver.v2.entities.match.BoolMatch;
	import com.smartfoxserver.v2.entities.match.MatchExpression;
	import com.smartfoxserver.v2.entities.match.RoomProperties;
	import com.smartfoxserver.v2.requests.ExtensionRequest;
	import com.smartfoxserver.v2.requests.FindRoomsRequest;
	import com.smartfoxserver.v2.requests.LoginRequest;
	
	import flash.events.Event;

	public class LoginLogic
	{
		private static var _instance	:LoginLogic;
		private var model:Modulator;
		private var userInfo:SFSUser;
		
		public static function get instance():LoginLogic
		{
			if(null==_instance)
			{
				_instance	=	new LoginLogic();
			}
			return _instance;
		}
		
		public function LoginLogic()
		{
			model	=	Modulator.instance;
			initEvent();
		}
		
		private function initEvent():void
		{
			NetSocket.instance.sfx.addEventListener(SFSEvent.LOGIN,onLogin);
			//NetSocket.instance.sfx.addEventListener(SFSEvent.LOGIN_ERROR,onLogin);
			model.addEventListener(LogicEvent.GETSERVER_INFO,onBeginServer);
			//NetSocket.instance.sfx.addEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionResponse);
			model.addEventListener(CustomToClientEvent.USER_INFO,onUserInfo);
		}
		
		/**
		 *  扩展回应
		 * @param event
		 * 
		 */
		protected function onUserInfo(event:CustomToClientEvent):void
		{
			var tmpObj	:SFSObject	=	event.pData;
			//trace(event);
			userInfo.properties["info"]	=	tmpObj;
			model.scene.addPlayer(model.playerId,userInfo);
			//TODO 测试用 如果用户为新用户 选择角色
			if(tmpObj.getBool("isNew"))
			{
				CreateRoleUI.getInstance().show();
				CreateRoleUI.getInstance().defaultName(tmpObj.getUtfString("nickname"));
			}
			else
			{
				NetSocket.send(new ExtensionRequest(CustomToServerRequest.SCENE_ENTER, null));
			}
		}
		
		/**
		 * 登陆成功 
		 * @param event
		 * 
		 */
		protected function onLogin(event:SFSEvent):void
		{
			trace("用户登陆好了");
			userInfo	=	event.params.user;
			//取得人物具体数据
			var params:ISFSObject = new SFSObject();
			params.putUtfString("openid", model.playerId);
			params.putUtfString("openkey", model.playerKey);
			NetSocket.send(new ExtensionRequest(CustomToServerRequest.USER_GET, params));
		}
		
		/**
		 * 开始取得服务器信息 
		 * 登陆进游戏
		 * @param event
		 * 
		 */
		protected function onBeginServer(event:Event):void
		{
			//临时用
			model.playerId	=	StaticIni.GetServerVar("agentConfig","openid");
			model.playerKey	=	StaticIni.GetServerVar("agentConfig","openkey");
			//用户先登陆游戏
			NetSocket.send(new LoginRequest(model.playerId,"uJJJXYJDadfaKGm2"));
		}
	}
}